#!/bin/bash
#2020年11月19日15:50:38
#auto config nginx virtual
#by author www.jfedu.net
#########################
NGX_CNF="nginx.conf"
NGX_DIR="/usr/local/nginx"
NGX_YUM="yum install -y"
NGX_URL="http://nginx.org/download"
NGX_ARG="--user=www --group=www --with-http_stub_status_module"
function nginx_help(){
	echo -e "\033[33mNginx VIrtual Manager SHELL Scripts\033[0m" 
	echo -e "\033[33m-------------------------------\033[0m" 
        echo -e "\033[33m1)-I New Install Nginx WEB Server.\033[0m" 
        echo -e "\033[33m2)-A v1.jfedu.net|v2.jfedu.net v3.jfedu.net\033[0m" 
        echo -e "\033[33m3)-D v1.jfedu.net|v2.jfedu.net v3.jfedu.net\033[0m" 
        echo -e "\033[35mUsage:{/bin/bash $0 -I(Install) -A(add) or -D(del) -H(help)\033[0m" 
        exit 0	
}
function nginx_install(){
#Nginx Install Config
if [ $# -le 1 ];then
        nginx_help
fi
if [ ! -d ${NGX_DIR} ];then
	shift 1
	NGX_VER=$(echo $*)
	NGX_SOFT="nginx-${NGX_VER}.tar.gz"
	NGX_SRC=$(echo $NGX_SOFT|sed 's/.tar.*//g')
	NGX_CODE="src/core/nginx.h"
	echo -e "\033[33m-------------------------------\033[0m" 
	echo -e "\033[33mStart Nginx install Proccess...\033[0m" 
	$NGX_YUM wget make gzip tar gcc gcc-c++ >>/dev/null 2>&1
	$NGX_YUM pcre pcre-devel zlib zlib-devel >>/dev/null 2>&1
	wget -c $NGX_URL/$NGX_SOFT
	tar -xzf $NGX_SOFT
	cd $NGX_SRC 
	sed -i "s/$NGX_VER//g" $NGX_CODE
	sed -i 's/nginx\//JWS/g' $NGX_CODE
	sed -i 's/"NGX"/"JWS"/g' $NGX_CODE
	useradd -s /sbin/nologin www -M
	./configure --prefix=${NGX_DIR}/ $NGX_ARG 
	make -j4
	make -j4 install
	${NGX_DIR}/sbin/nginx
	ps -ef|grep -aiwE nginx
	netstat -tnlp|grep -aiwE 80
	setenforce 0
	sed -i '/SELINUX/s/enforcing/disabled/g' /etc/sysconfig/selinux
	if [ $(uname -r|awk -F"[-|.]" '{print $1}') -ge "3" ];then
		firewall-cmd --add-port=80/tcp --permanent
		systemctl reload firewalld.service
	else
		iptables -t filter -A INPUT -m tcp -p tcp --dport 80 -j ACCEPT
		service iptables save
	fi
else
	echo -e "\033[32mThe Nginx WEB Already Install,Please Exit.\033[0m" 
	echo -e "\033[33m-----------------------\033[0m"
	echo "ls -l $NGX_DIR/"
	ls -l $NGX_DIR/

	echo -e "\033[33m-----------------------\033[0m"
	while true
	do
	echo -e -n "\033[33mPlease ensure to retry Nginx WEB service,yes or no ? \033[0m"
	read INPUT
	if [ -z $INPUT ];then
		continue
	fi
	if [ $INPUT == "yes" -o $INPUT == "YES" -o $INPUT == "y" ];then
		echo -e "-------------------------------"
		echo -e "Backup nginx to ${NGX_DIR}.bak,\mv $NGX_DIR ${NGX_DIR}.bak"
		\mv $NGX_DIR ${NGX_DIR}.bak
		shift 1
        	NGX_VER=$(echo $*)
        	NGX_SOFT="nginx-${NGX_VER}.tar.gz"
        	NGX_SRC=$(echo $NGX_SOFT|sed 's/.tar.*//g')
        	NGX_CODE="src/core/nginx.h"
        	echo -e "\033[33m-------------------------------\033[0m" 
        	echo -e "\033[33mStart Nginx install Proccess...\033[0m" 
        	$NGX_YUM wget make gzip tar gcc gcc-c++ >>/dev/null 2>&1
        	$NGX_YUM pcre pcre-devel zlib zlib-devel >>/dev/null 2>&1
        	wget -c $NGX_URL/$NGX_SOFT
        	tar -xzf $NGX_SOFT
        	cd $NGX_SRC
        	sed -i "s/$NGX_VER//g" $NGX_CODE
        	sed -i 's/nginx\//JWS/g' $NGX_CODE
        	sed -i 's/"NGX"/"JWS"/g' $NGX_CODE
        	useradd -s /sbin/nologin www -M
        	./configure --prefix=${NGX_DIR}/ $NGX_ARG
        	make -j4
        	make -j4 install
        	${NGX_DIR}/sbin/nginx
        	ps -ef|grep -aiwE nginx
        	netstat -tnlp|grep -aiwE 80
        	setenforce 0
        	sed -i '/SELINUX/s/enforcing/disabled/g' /etc/sysconfig/selinux
        	if [ $(uname -r|awk -F"[-|.]" '{print $1}') -ge "3" ];then
        	        firewall-cmd --add-port=80/tcp --permanent
        	        systemctl reload firewalld.service
        	else
        	        iptables -t filter -A INPUT -m tcp -p tcp --dport 80 -j ACCEPT
        	        service iptables save
        	fi
		break
		
	fi
	done
fi
}

function virtual_add(){
	#Nginx Config Virtual Host
	if [ $# -le 1 ];then
		nginx_help
	fi
	cd ${NGX_DIR}/conf/
	grep -aiE "include domains" ${NGX_CNF} >>/dev/null 2>&1
	if [ $? -ne 0 ];then
		grep -aiE -vE "^$|#" ${NGX_CNF} > ${NGX_CNF}.swp
		\cp ${NGX_CNF}.swp ${NGX_CNF}
		sed -i '/server/,$d' ${NGX_CNF}
		echo -e -e "    include domains/*;\n}" >>${NGX_CNF}
		${NGX_DIR}/sbin/nginx -t
		mkdir domains -p
	fi
	shift 1
	for Q in $*
	do
		NUM=`ls domains/|grep -aiE -c $Q`
		if [ $NUM -eq 0 ];then
		cat>domains/$Q<<-EOF
		server {
		        listen       80;
		        server_name  $Q;
		        location / {
		            root   html/$Q;
		            index  index.html index.htm;
		        }
		}
		EOF
		mkdir -p ${NGX_DIR}/html/$Q
		cat>${NGX_DIR}/html/$Q/index.html<<-EOF
		<h1>$* Welcome to nginx!</h1>
		<hr color=red>
		EOF
		echo -e "\033[32m-----------------------\033[0m" 
		echo -e "\033[32mThe Nginx $Q ADD Success.\033[0m" 
		cat domains/$Q
		echo -e "\033[32m-----------------------\033[0m" 
		$NGX_DIR/sbin/nginx -t
		$NGX_DIR/sbin/nginx -s reload
		echo
		else
			echo -e "\033[32m-----------------------\033[0m" 
			echo -e "\033[32mThe Nginx $Q Already Exist,Please Exit.\033[0m" 
			cat domains/$Q
		fi
	done
}

function virtual_del(){
	if [ $# -le 1 ];then
        	nginx_help
	fi
	cd ${NGX_DIR}/conf/domains/ >/dev/null 2>&1
	if [ $? -eq 0 ];then
		shift 1
		ls -l|grep -aiE "$*" >/dev/null 2>&1 
		if [ $? -eq 0 ];then
			cat $*
			if [ $? -eq 0 ];then 
				mkdir -p /data/backup/`date +%F`
				\cp -a $* /data/backup/`date +%F`
				rm -rf $*
				$NGX_DIR/sbin/nginx -s reload
				echo -e "\033[32m-----------------------\033[0m"
				echo -e "\033[32mThe Nginx $* Already remove,reload nginx...\033[0m"
			fi
		else
			shift 1
                	echo -e "\033[31m-----------------------\033[0m"
                	echo -e "\033[31mNginx $* Virtual hosts does not exist.please check.\033[0m"    
                	ls -l $NGX_DIR/conf/
                	exit 1
		fi
	else
		shift 1
		echo -e "\033[31m-----------------------\033[0m"
		echo -e "\033[31mNginx $* Virtual hosts does not exist.please check.\033[0m"	
		ls -l $NGX_DIR/conf/
		exit 1
	fi
}
case $1 in
	-i|-I)
	nginx_install $* 
	;;
	-a|-A)
	virtual_add $*
	;;
	-a|-D)
	virtual_del $*
	;;
	* )
	nginx_help
	;;
esac
